package com.shop2cn.iapi.agentproducts.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.BatchUpdateStockBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.OpenUpdateStockDto;
import com.shop2cn.iapi.agentproducts.service.BatchUpdateStockCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.AgentUsers;

/**
*批量更新商品库存
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BatchUpdateStock{
	private static BatchUpdateStockBean batchupdatestockBean;
	private static BatchUpdateStockCall batchupdatestockCall;
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	//渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	//渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	//渠道商->代理商2
	private static int agentUserId2 = AgentUsers.agentUserId2;
	private static String agentUserName2 = "NickautoTest";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量更新商品库存");
	}

	@Before
	public void caseUp() {
		batchupdatestockBean=new BatchUpdateStockBean();
		batchupdatestockCall=new BatchUpdateStockCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchupdatestockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchUpdateStock_01() {
		Logger.start(true,"根据商品sku更新商品库存,更新成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			List<OpenUpdateStockDto> updateStockList = new ArrayList<>();
			OpenUpdateStockDto openUpdateStockDto = new OpenUpdateStockDto();
			openUpdateStockDto.setOuterSku(addAgentProductBean.getCatalogList().get(0).getSku());
			int stock = 50;
			openUpdateStockDto.setStockNum(stock);
			updateStockList.add(openUpdateStockDto);
			batchupdatestockBean.setUpdateStockList(updateStockList);
			batchupdatestockBean.setUserId(rootUserId);
			batchupdatestockCall.setData(batchupdatestockBean);
			batchupdatestockCall.callService();			
			Logger.verifyEquals("200",batchupdatestockCall.getString("code"),"验证code");
			List<Map> cmap = ymtCatalogsIWapper.selectProductIdBySku(openUpdateStockDto.getOuterSku());
			cmap.forEach(p->{
				Logger.verifyEquals(stock,Integer.parseInt(p.get("iNum").toString()),"验证库存是否更新");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchUpdateStock_02() {
		Logger.start(true,"根据商品规格id更新商品库存,更新成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<YmtCatalogsI> ymtCatalogsIList = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			List<OpenUpdateStockDto> updateStockList = new ArrayList<>();
			int stock = 50;
			ymtCatalogsIList.forEach(p->{
				OpenUpdateStockDto openUpdateStockDto = new OpenUpdateStockDto();
				openUpdateStockDto.setCatalogId(p.getsCatalogId());
				openUpdateStockDto.setStockNum(stock);
				updateStockList.add(openUpdateStockDto);
			});
			batchupdatestockBean.setUpdateStockList(updateStockList);
			batchupdatestockBean.setUserId(rootUserId);
			batchupdatestockCall.setData(batchupdatestockBean);
			batchupdatestockCall.callService();
			Logger.verifyEquals("200",batchupdatestockCall.getString("code"),"验证code");
			List<YmtCatalogsI> ymtCatalogsIList1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			ymtCatalogsIList1.forEach(p->{
				Logger.verifyEquals(stock,p.getiNum(),"验证库存是否更新");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchUpdateStock_03() {
		Logger.start(true,"userId为空，更新失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<YmtCatalogsI> ymtCatalogsIList = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			List<OpenUpdateStockDto> updateStockList = new ArrayList<>();
			int stock = -1;
			ymtCatalogsIList.forEach(p->{
				OpenUpdateStockDto openUpdateStockDto = new OpenUpdateStockDto();
				openUpdateStockDto.setCatalogId(p.getsCatalogId());
				openUpdateStockDto.setStockNum(stock);
				updateStockList.add(openUpdateStockDto);
			});
			batchupdatestockBean.setUpdateStockList(updateStockList);
			batchupdatestockCall.setData(batchupdatestockBean);
			batchupdatestockCall.callService();
			Logger.verifyEquals("400",batchupdatestockCall.getString("code"),"验证code");
			List<YmtCatalogsI> ymtCatalogsIList1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			ymtCatalogsIList1.forEach(p->{
				Logger.verifyEquals(true,p.getiNum()!=stock,"验证库存是否更新");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchUpdateStock_05() {
		Logger.start(true,"stockNum小于0，更新失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<YmtCatalogsI> ymtCatalogsIList = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			List<OpenUpdateStockDto> updateStockList = new ArrayList<>();
			int stock = -1;
			ymtCatalogsIList.forEach(p->{
				OpenUpdateStockDto openUpdateStockDto = new OpenUpdateStockDto();
				openUpdateStockDto.setCatalogId(p.getsCatalogId());
				openUpdateStockDto.setStockNum(stock);
				updateStockList.add(openUpdateStockDto);
			});
			batchupdatestockBean.setUpdateStockList(updateStockList);
			batchupdatestockBean.setUserId(rootUserId);
			batchupdatestockCall.setData(batchupdatestockBean);
			batchupdatestockCall.callService();
			Logger.verifyEquals("200",batchupdatestockCall.getString("code"),"验证code");
			List<YmtCatalogsI> ymtCatalogsIList1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			ymtCatalogsIList1.forEach(p->{
				Logger.verifyEquals(true,p.getiNum()!=stock,"验证库存是否更新");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchUpdateStock_06() {
		Logger.start(true,"sku id无效，更新失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			List<OpenUpdateStockDto> updateStockList = new ArrayList<>();
			OpenUpdateStockDto openUpdateStockDto = new OpenUpdateStockDto();
			openUpdateStockDto.setOuterSku("11");
			int stock = 50;
			openUpdateStockDto.setStockNum(stock);
			updateStockList.add(openUpdateStockDto);
			batchupdatestockBean.setUpdateStockList(updateStockList);
			batchupdatestockBean.setUserId(rootUserId);
			batchupdatestockCall.setData(batchupdatestockBean);
			batchupdatestockCall.callService();
			Logger.verifyEquals("200",batchupdatestockCall.getString("code"),"验证code");
			List<YmtCatalogsI> ymtCatalogsIList1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			ymtCatalogsIList1.forEach(p->{
				Logger.verifyEquals(true,p.getiNum()!=stock,"验证库存是否更新");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchUpdateStock_07() {
		Logger.start(true,"代理商商品更改库存，更新失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,rootUserId,false);
			String agengtProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			List<OpenUpdateStockDto> updateStockList = new ArrayList<>();
			OpenUpdateStockDto openUpdateStockDto = new OpenUpdateStockDto();
			openUpdateStockDto.setOuterSku(importAgentProductBean.getCatalogList().get(0).getSku());
			int stock = 50;
			openUpdateStockDto.setStockNum(stock);
			updateStockList.add(openUpdateStockDto);
			batchupdatestockBean.setUpdateStockList(updateStockList);
			batchupdatestockBean.setUserId(agentUserId1);
			batchupdatestockCall.setData(batchupdatestockBean);
			batchupdatestockCall.callService();
			Logger.verifyEquals("200",batchupdatestockCall.getString("code"),"验证code");
			List<YmtCatalogsI> ymtCatalogsIList1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			ymtCatalogsIList1.forEach(p->{
				Logger.verifyEquals(true,p.getiNum()!=stock,"验证库存是否更新");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchUpdateStock_08() {
		Logger.start(true,"商品规格id无效，更改失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<YmtCatalogsI> ymtCatalogsIList = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			List<OpenUpdateStockDto> updateStockList = new ArrayList<>();
			int stock = 50;
			ymtCatalogsIList.forEach(p->{
				OpenUpdateStockDto openUpdateStockDto = new OpenUpdateStockDto();
				openUpdateStockDto.setCatalogId(p.getsCatalogId()+1);
				openUpdateStockDto.setStockNum(stock);
				updateStockList.add(openUpdateStockDto);
			});
			batchupdatestockBean.setUpdateStockList(updateStockList);
			batchupdatestockBean.setUserId(rootUserId);
			batchupdatestockCall.setData(batchupdatestockBean);
			batchupdatestockCall.callService();
			Logger.verifyEquals("200",batchupdatestockCall.getString("code"),"验证code");
			List<YmtCatalogsI> ymtCatalogsIList1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			ymtCatalogsIList1.forEach(p->{
				Logger.verifyEquals(true,p.getiNum()!=stock,"验证库存是否更新");
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchUpdateStock_09() {
		Logger.start(true,"同时传入规格id和skuid，以catalogId为准，更新成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<YmtCatalogsI> ymtCatalogsIList = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			List<OpenUpdateStockDto> updateStockList = new ArrayList<>();
			int stock = 50;
			ymtCatalogsIList.forEach(p->{
				OpenUpdateStockDto openUpdateStockDto = new OpenUpdateStockDto();
				openUpdateStockDto.setCatalogId(p.getsCatalogId());
				openUpdateStockDto.setStockNum(stock);
				openUpdateStockDto.setOuterSku("11");
				updateStockList.add(openUpdateStockDto);
			});
			batchupdatestockBean.setUpdateStockList(updateStockList);
			batchupdatestockBean.setUserId(rootUserId);
			batchupdatestockCall.setData(batchupdatestockBean);
			batchupdatestockCall.callService();
			Logger.verifyEquals("200",batchupdatestockCall.getString("code"),"验证code");
			List<YmtCatalogsI> ymtCatalogsIList1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			ymtCatalogsIList1.forEach(p->{
				Logger.verifyEquals(stock,p.getiNum(),"验证库存是否更新");
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}